package com.cheney.weiboo.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cheney.weiboo.R;
import com.mime.qweibo.examples.MyWeiboSync;

public class AuthorizeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibview);
		Log.d("授权","授权");
		WebView wv = (WebView)findViewById(R.id.webView);
		wv.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				Pattern p = Pattern.compile("^" + MyWeiboSync.CALLBACK_URL
						+ ".*oauth_verifier=(\\d+)");
				Matcher m = p.matcher(url);
				System.out.println(url);
				if (m.find()) {
					Intent intent = new Intent();
					intent.setAction("com.cheney.weibo.getVerifier");
					String veryfier = m.group(1);
					intent.putExtra("veryfier", veryfier);
					sendBroadcast(intent);
					finish();
				}
			}
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		Intent intent = this.getIntent();
		if(intent!=null){
			Bundle bundle = intent.getExtras();
			if(bundle!=null){
				if(bundle.containsKey("url")){
					String url = bundle.getString("url");
					wv.getSettings().setJavaScriptEnabled(true);
					wv.getSettings().setSupportZoom(true);
					wv.requestFocus();
					wv.loadUrl(url);
				}
			}
		}
	}
}
